package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.mapper.StrategyMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.UploadUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class StrategyServiceImpl implements IStrategyService {
    @Autowired
    private StrategyMapper strategyMapper;

    /**
     * 查询推荐游记
     * @param qo 查询条件
     * @return
     */
    @Override
    public PageInfo<Strategy> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(), true, false, null);
        PageHelper.orderBy(qo.getOrderBy());
        List<Strategy> strategies = strategyMapper.selectForList(qo);
        return new PageInfo<>(strategies);
    }

    @Override
    public void saveOrUpdate(Strategy commend, MultipartFile file) {
        if (file != null && file.getSize() > 0) {
            String fileName = UploadUtil.uploadQiniu(file);
            commend.setCoverUrl(fileName);
        }
        if (commend.getId() != null) {
            strategyMapper.updateByPrimaryKey(commend);
        }else {
            strategyMapper.insert(commend);
        }
    }

    @Override
    public List<Strategy> selectAll() {
        return strategyMapper.selectAll();
    }

    /*@Override
    public List<Strategy> selectByPlaceId(Long placeId) {
        return strategyMapper.selectByPlaceId(placeId);
    }*/
}
